package com.lishe.lishe.lishe.module.auth.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.text.InputFilter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lishe.lishe.R;
import com.lishe.lishe.asynchttp.LSWebHelper;
import com.lishe.lishe.lishe.module.auth.http.AuthHttpHelper;
import com.lishe.lishe.lishe.module.auth.view.SendCodeTextView;
import com.lishe.lishe.lishe.utils.LoadingUtils;
import com.lishe.lishe.lishe.utils.ValidationUtils;

import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by zhcase on 2017/9/5.
 */

public class RegFragment extends AuthBaseFragment implements View.OnClickListener {

    public static final String TAG = RegFragment.class.getSimpleName();

    ViewPager viewPager = null;

    private EditText inputText1, inputText2;

    private SendCodeTextView mInputText_sendCode;



    public RegFragment() {
    }
    public static RegFragment newInstance(){
        RegFragment instance = new RegFragment();
        Bundle bundle = new Bundle();
        instance.setArguments(bundle);
        return instance;
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View containerFragment = inflater.inflate(R.layout.activity_auth_reg, container, false);

        ImageButton back = (ImageButton) containerFragment.findViewById(R.id.back_title);
        back.setOnClickListener(this);

        inputText1 = (EditText) containerFragment.findViewById(R.id.inputText1);
        inputText1.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
        inputText1.setText("");

        inputText2 = (EditText) containerFragment.findViewById(R.id.inputText2);
        inputText2.setText("");

        Button submitBtn = (Button) containerFragment.findViewById(R.id.submitBtn);
        submitBtn.setOnClickListener(this);

        TextView redirectBtn1 = (TextView) containerFragment.findViewById(R.id.redirectBtn1);
        redirectBtn1.setClickable(true);
        redirectBtn1.setOnClickListener(this);

        mInputText_sendCode = (SendCodeTextView) containerFragment.findViewById(R.id.inputText_sendCode);
        mInputText_sendCode.setClickable(true);
        mInputText_sendCode.setOnClickListener(this);

        //AuthHttpHelper.phoneOfReg = null;

        //Log.e(TAG,"onCreateView:"+AuthHttpHelper.phoneOfReg);


        return containerFragment;
    }

    @Override
    public void onClick(View view) {

        if (view.getId() == R.id.back_title) {
//            if (viewPager != null) {
//                viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_Login, true);
//            }
            if(mJumper!=null)
                mJumper.jumpToFragmentBack(FragmentAuthContants.TAB_INDEX_Login);

        } else if (view.getId() == R.id.redirectBtn1) {

            //用户协议

            if(mJumper!=null)
                mJumper.jumpToFragmentForward(FragmentAuthContants.TAB_INDEX_UserProtocol);

        } else if (view.getId() == R.id.inputText_sendCode) {

            //获取验证码

            AuthHttpHelper.phoneOfReg = null;

            inputText2.setText("");




            final String phone = inputText1.getText().toString().trim();

            if (phone == null || phone.equals("")) {
                Toast.makeText(getActivity(), "请输入手机号码", Toast.LENGTH_SHORT).show();
                return;
            }

            //验证手机号码
            if (!ValidationUtils.isMobileNO(phone)) {
                Toast.makeText(getActivity(), "手机号码格式有误！", Toast.LENGTH_SHORT).show();
                return;
            }
            HashMap<String, Object> params = new HashMap<String, Object>();
            params.put("mobile", phone);
            AuthHttpHelper.doCheckSignUp(getActivity(), params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {

                    AuthHttpHelper.phoneOfReg = phone;
                    mInputText_sendCode.startCountDown();
                }

                @Override
                public void onFailure(String message) {
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                    mInputText_sendCode.prepareReSendCode();
                }
            });


        } else if (view.getId() == R.id.submitBtn) {

            final String phone = AuthHttpHelper.phoneOfReg;
            final String checkCode = inputText2.getText().toString().trim();
            String phone_input = inputText1.getText().toString().trim();//手机号码输入框


            if(phone_input == null || phone_input.equals("")||!ValidationUtils.isMobileNO(phone_input))
            {

                Toast.makeText(getActivity(), "手机号码格式有误!", Toast.LENGTH_SHORT).show();
                return;
            }

            if(checkCode == null || checkCode.equals(""))
            {
                Toast.makeText(getActivity(), "验证码不能为空！", Toast.LENGTH_SHORT).show();

                return;
            }

            if(phone_input == null ||!phone_input.equals(phone))
            {

                Toast.makeText(getActivity(), "发送验证码的手机号码与当前号码不符！", Toast.LENGTH_SHORT).show();

                return;
            }

            LoadingUtils.createLoadingDialog(getActivity(),"请稍等...",true);

            HashMap params = new HashMap();
            params.put("mobile",phone);
            params.put("verify",checkCode);
            AuthHttpHelper.doCheckRegisterMsgVerifyCode(getActivity(),params, new LSWebHelper.WebCallBack() {
                @Override
                public void onSuccess(JSONObject dataJson, String dataString, String message) {

                    LoadingUtils.closeDialog();
                    //viewPager.setCurrentItem(FragmentAuthContants.TAB_INDEX_RegEnd,true);

                    //Log.e(TAG,"doCheckRegisterMsgVerifyCode:"+AuthHttpHelper.phoneOfReg);
                    inputText1.setText("");
                    inputText2.setText("");
                    if(mJumper!=null)
                    {
                        Log.e(TAG,AuthHttpHelper.phoneOfReg);
                        mJumper.jumpToFragmentForward(FragmentAuthContants.TAB_INDEX_RegEnd);
                    }


                }

                @Override
                public void onFailure(String message) {
                    LoadingUtils.closeDialog();
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
                }
            });


        }
    }

}
